﻿using System;
using System.Collections.Generic;
using System.Text;
using MigratorCore.Actions;
using MigratorCore.Bases;
using MigratorCore;
using MigratorCore.Objects;
using MigratorCore.Status;
using System.Data.OleDb;
using System.Data;

namespace Migrator.WebWizForums.version7_9
{
    public class ForumSource: ISourceAction
    {

        #region ISourceAction Members

        public void Get()
        {
            objects = new ObjectSet();
            status.AddEvent(new Event(EventType.Information, "Initializing", "Initializing Action", DateTime.Now));
            OleDbConnection con = new OleDbConnection(settings["ConnectionString"].ToString());
            OleDbDataReader read;
            #region Get Structure and test data connection
            OleDbCommand sc = new OleDbCommand("SELECT tblCategory.Cat_name, tblForum.Forum_name, tblForum.Forum_description FROM tblCategory INNER JOIN tblForum ON tblCategory.Cat_ID = tblForum.Cat_ID;", con);
            try
            {
                con.Open();
                read = sc.ExecuteReader(System.Data.CommandBehavior.CloseConnection);
            }
            catch (Exception e)
            {
                status.Completed = 100;
                status.AddEvent(new Event(EventType.CriticalError, "Data Connection Failed!", e.Message, e, DateTime.Now));
                return;
            }
            int x = 0;
            status.AddEvent(new Event(EventType.Information, "Loading Forum Structre", "Loaded Forum Structre from Source", DateTime.Now));
            while (read.Read())
            {
                x++;
                bool added = false;
                foreach (Forum forum in this.Objects)
                    if (forum.Name.Equals(read.GetString(0)))
                    {
                        Forum f = new Forum(read.GetString(1), read.GetString(2));
                        forum.Forums.Add(f);
                        added = true;
                    }
                if (!added)
                {
                    Forum f = new Forum(read.GetString(0), "");
                    Forum f2 = new Forum(read.GetString(1), read.GetString(2));
                    f.Forums = new List<Forum>();
                    f.Forums.Add(f2);
                    this.Objects.Add(f);
                }

            }
            sc.Dispose();
            status.AddEvent(new Event(EventType.Information, "Loaded " + x.ToString() + " Forums and Groups", "Loaded " + x.ToString() + " Forums, Sub Forums and Groups from Source", DateTime.Now));
            #endregion
            #region Get Forum Posts
            x = 0;
            status.AddEvent(new Event(EventType.Information, "Loaded Posts", "Loading posts from Source", DateTime.Now));
            OleDbDataAdapter apt = new OleDbDataAdapter("SELECT tblForum.Forum_name, tblTopic.Subject, tblThread.Message, tblThread.Message_date, tblAuthor.Username, tblAuthor.Author_email FROM tblForum INNER JOIN (tblAuthor INNER JOIN (tblTopic INNER JOIN tblThread ON tblTopic.Topic_ID = tblThread.Topic_ID) ON tblAuthor.Author_ID = tblThread.Author_ID) ON tblForum.Forum_ID = tblTopic.Forum_ID ORDER BY tblThread.Message_date;", settings["ConnectionString"].ToString());
            DataSet ds = new DataSet();
            apt.Fill(ds);
            foreach (DataRow row in ds.Tables[0].Rows)
            {
                x++;
                Post p = new Post();
                p.Subject = row[1].ToString();
                p.Body = row[2].ToString();
                p.Posted = (DateTime)row[3];
                p.User = new User(row[4].ToString(), row[5].ToString().Replace("mailto:", ""));
                foreach (Forum forum in this.Objects)
                    parsePost(p, forum, row[0].ToString());
                double aa = 0.00 + ds.Tables[0].Rows.Count;
                status.Completed = Convert.ToInt32(Math.Round(Convert.ToDecimal(((x + 1) / aa) * 100), 0));
                status.AddEvent(new Event(EventType.Information, "Processed Post " + row["Subject"].ToString(), "Processed Post " + row["Subject"].ToString(), DateTime.Now));
            }
            #endregion
        }

        #endregion

        private void parsePost(Post post, Forum forum, string forumname)
        {
            if (forum.Name.Equals(forumname))
            {
                bool added = false;
                foreach (Post p in forum.Posts)
                    if (post.Subject == p.Subject)
                    {
                        p.Posts.Add(post);
                        added = true;
                    }
                if (!added)
                    forum.Posts.Add(post);
            }
            else if (forum.Forums.Count > 0)
            {
                foreach (Forum f in forum.Forums)
                    parsePost(post, f, forumname);
            }
        }

        #region IAction Members

        public string Name
        {
            get { return "Forums Source"; }
        }

        private Status status;
        public Status Status
        {
            get
            {
                if (status == null)
                    status = new Status();
                return status;
            }
        }

        private ObjectSet objects;
        public ObjectSet Objects
        {
            get
            {
                if (this.objects == null) this.objects = new ObjectSet();
                return this.objects;
            }
            set
            {
                this.objects = value;
            }
        }

        public ActionType ActionType
        {
            get { return ActionType.Forums; }
        }

        private Settings settings;
        public Settings Settings
        {
            get
            {
                if (settings == null)
                {
                    settings = new Settings();
                    settings.Add("ConnectionString", "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\\Databases\\www.mdb;User Id=admin;Password=;");
                }
                return settings;
            }
        }

        #endregion
    }
}
